/**
 * 
 */
package com.thinkgsp.sys.service.impl;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.thinkgsp.jxc.service.ServiceException;
import com.thinkgsp.sys.model.SysNumberRole;
import com.thinkgsp.sys.persistence.NumberRoleMapper;
import com.thinkgsp.sys.service.NumberRoleService;

/**
 * @author evan
 *
 */
@Service("numberRoleService")
public class NumberRoleServiceImpl extends BaseServiceImpl implements
		NumberRoleService {
	@Autowired
	private NumberRoleMapper numberRoleMapper;
	
	public void setNumberRoleMapper(NumberRoleMapper numberRoleMapper) {
		this.numberRoleMapper = numberRoleMapper;
	}
	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.service.NumberRoleService#getNumberRole(java.lang.Integer)
	 */
	public SysNumberRole getNumberRole(Integer roleId) {
		return numberRoleMapper.getNumberRoleForEdit(roleId);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.service.NumberRoleService#getAllRoles()
	 */
	public List<SysNumberRole> getAllRoles() {
		return numberRoleMapper.getAllNumberRole();
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.service.NumberRoleService#saveNumberRole(com.thinkgsp.sys.model.SysNumberRole)
	 */
	public void saveNumberRole(SysNumberRole role) {
		if(role.getId() == null) {
			numberRoleMapper.insertNumberRole(role);
		} else {
			numberRoleMapper.updateNumberRole(role);
		}

	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.sys.service.NumberRoleService#newNumber(java.lang.Integer)
	 */
	public String newNumber(Integer roleId) throws ServiceException {
		SysNumberRole role = numberRoleMapper.getNumberRoleForNewVal(roleId);
		Calendar refDate = Calendar.getInstance();
		refDate.setTime(role.getRefDate());
		
		Calendar curDate = Calendar.getInstance();
		Date curTime = new Date(System.currentTimeMillis());
		curDate.setTime(curTime);
				
		if(SysNumberRole.INCREMENT_BY_YEAR.equals(role.getIncrementType())) {
			if(refDate.get(Calendar.YEAR) != curDate.get(Calendar.YEAR)) {
				role.setRefDate(curTime);
				role.setCurVal(0L);
				numberRoleMapper.updateRefDate(roleId, curTime);
			}
		} else if(SysNumberRole.INCREMENT_BY_MONTH.equals(role.getIncrementType())) {
			if(refDate.get(Calendar.MONTH) != curDate.get(Calendar.MONTH)) {
				role.setRefDate(curTime);
				role.setCurVal(0L);
				numberRoleMapper.updateRefDate(roleId, curTime);
			}
		} else if(SysNumberRole.INCREMENT_BY_DAY.equals(role.getIncrementType())) {
			if(refDate.get(Calendar.DAY_OF_MONTH) != refDate.get(Calendar.DAY_OF_MONTH)) {
				role.setRefDate(curTime);
				role.setCurVal(0L);
				numberRoleMapper.updateRefDate(roleId, curTime);
			}
		} 
		
		int cnt = 0;
		int index = 0;
		String format = role.getRoleFormat();
		do {
			index = format.indexOf("%t", index);
			if(index >= 0) {
				cnt++;
				index++;
			}
		} while(index != -1);
		long serialValue = role.getIncrementVal().intValue() + role.getInitVal().intValue() + role.getCurVal().longValue();
		String newCode = String.format("%011d", serialValue);
		switch(cnt) {
		case 0 :
			newCode = String.format(role.getRoleFormat(),serialValue);
			break;
		case 1:
			newCode = String.format(role.getRoleFormat(), role.getRefDate(),serialValue);
			break;
		case 2:
			newCode = String.format(role.getRoleFormat(), role.getRefDate(),role.getRefDate(),serialValue);
			break;	
		case 3:
			newCode = String.format(role.getRoleFormat(), role.getRefDate(),role.getRefDate(),role.getRefDate(),serialValue);
			break;	
		case 4:
			newCode = String.format(role.getRoleFormat(), role.getRefDate(),role.getRefDate(),role.getRefDate(),role.getRefDate(),serialValue);
			break;	
		case 5:
			newCode = String.format(role.getRoleFormat(), role.getRefDate(),role.getRefDate(),role.getRefDate(),role.getRefDate(),role.getRefDate(),serialValue);
			break;	
		case 6:
			newCode = String.format(role.getRoleFormat(), role.getRefDate(),role.getRefDate(),role.getRefDate(),role.getRefDate(),role.getRefDate(),role.getRefDate(),serialValue);
			break;	
		default:
			throw new ServiceException("编码规则格式化信息无效：" + role.getRoleFormat());
		}
		//String newCode = String.format(role.getRoleFormat(), role.getRefDate(),role.getRefDate(),role.getRefDate(),serialValue);
		numberRoleMapper.updateCurVal(roleId, serialValue);
		return newCode;
	}
	public Long newId(Integer roleId) throws ServiceException {
		String code = newNumber(roleId);
		try {
			return Long.parseLong(code);
		} catch(Exception e) {
			throw new ServiceException("获取Id出错:" + e.getMessage());
		}
	}

}
